package com.huixuebao.after.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.huixuebao.after.dto.*;
import com.huixuebao.after.entity.DictionaryEntity;
import com.huixuebao.after.mapper.DictionaryMapper;
import com.huixuebao.after.service.DictionaryService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    DictionaryMapper dictionaryMapper;
    @Override
    public ApiResult<List<DictionaryDTO>> queryDictionaryByPid(String pid) {
        if (pid == null || "".equals(pid)) {
            pid = "0";
        }
        List<DictionaryEntity> dictionaryList = dictionaryMapper.queryDivisionByPid(pid);
        ApiResult apiResult = new ApiResult();
        apiResult.setData(dictionaryList);
        return apiResult;
    }

    @Override
    public ApiResult<List<DictionaryEntity>> dictionaryTree() {
        ApiResult apiResult = new ApiResult();
        try {
            DictionaryDTO cad  = new DictionaryDTO();
            List<DictionaryEntity> startList= dictionaryMapper.selectList(null);
            List<DictionaryCommonDTO> dictionaryFieldDtos = startList.stream().filter(l->"0".equals(l.getPid())).map((item)->{
                DictionaryCommonDTO dfd = new DictionaryCommonDTO();
                BeanUtil.copyProperties(item,dfd);
                return dfd;
            }).collect(Collectors.toList());
            for (DictionaryCommonDTO dictionaryFieldDto: dictionaryFieldDtos) {
                List<DictionaryCommonDTO> ll= startList.stream().filter(l->dictionaryFieldDto.getId().equals(l.getPid())).map(item->{
                    DictionaryCommonDTO dfd = new DictionaryCommonDTO();
                    BeanUtil.copyProperties(item,dfd);
                    return dfd;
                }).collect(Collectors.toList());
                dictionaryFieldDto.setChildren(ll);
                switch (dictionaryFieldDto.getName()){
                    case "职位":
                        Position position = new Position();
                        BeanUtil.copyProperties(dictionaryFieldDto,position);
                        cad.setPosition(position);
                        break;
                    case "学龄阶段":
                        SchoolAge schoolAge = new SchoolAge();
                        BeanUtil.copyProperties(dictionaryFieldDto,schoolAge);
                        cad.setSchoolAge(schoolAge);
                        break;
                    case "授课方式":
                        Teachingmethod tt=new Teachingmethod();
                        BeanUtil.copyProperties(dictionaryFieldDto,tt);
                        cad.setTeachingmethod(tt);
                        break;
                    case "班型":
                        ClassType classTypeDTO=new ClassType();
                        BeanUtil.copyProperties(dictionaryFieldDto,classTypeDTO);
                        cad.setClassType(classTypeDTO);
                        break;
                    default:
                }
            }
            apiResult.setData(cad);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取课程分类成功!!");
        } catch (Exception e) {
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取课程分类失败!!");
        }
        return apiResult;
    }
}
